package com.berchen.config;

import com.berchen.interceptor.LoginInterceptor;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.CorsRegistry;
import org.springframework.web.servlet.config.annotation.InterceptorRegistration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.util.ArrayList;
import java.util.List;

/**
 * 登录验证的拦截器的配置
 */
@Configuration
public class InterceptorConfig implements WebMvcConfigurer {

    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {

        InterceptorRegistration interceptor = registry.addInterceptor(loginInterceptor);
        List<String> include=new ArrayList<>();
        include.add("/**");
        interceptor.addPathPatterns(include);

        List<String> exclude=new ArrayList<>();
        exclude.add("/login/send/**");
        exclude.add("/login/login/**");
        exclude.add("/file/**");
        exclude.add("/test/**");
        exclude.add("/test/exception");
        exclude.add("/busi/download/**");
        interceptor.excludePathPatterns(exclude);
    }

    /**
     * 解决跨域问题  使用了这个解决跨域问题  就不需要使用注解@CrossOrigin()来解决了
     * 两者选其一即可
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowCredentials(true)
                .allowedHeaders("*")
                .allowedOriginPatterns("*")
                .allowedMethods("*");
    }
}
